module view {

    export class FuseView extends eui.Component {
        closeBtn: eui.Button
        item1Label: eui.Label
        item2Label: eui.Label
        item3Label: eui.Label
        item1Btn: eui.Button
        item2Btn: eui.Button
        item3Btn: eui.Button
        itemsGroup: eui.Group
        bagBtn: eui.Button
        storeBtn: eui.Button
        selectPackage: game.Package
        fuseBtn: eui.Button
        fuseProgressBar: eui.ProgressBar
		public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/FuseSkin.exml"
		}
		
        private onComplete(): void {
            this.bagBtn['value'] = gplayer.bag.type
            this.storeBtn['value'] = gplayer.store.type
            this.bagBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.packageTouch,this)            
            this.storeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.packageTouch,this) 
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)     
            this.fuseBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.startFuse,this)  
            this.item1Btn['value'] = 0
            this.item2Btn['value'] = 1
            this.item3Btn['value'] = 2
            this.item1Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.removeItem,this) 
            this.item2Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.removeItem,this) 
            this.item3Btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.removeItem,this) 
            this.showItems(gplayer.bag)
            this.fuseInfo()
        }
        
        private close(): void {
            var child = new view.SmithyView()
            this.stage.addChild(child)
            this.parent.removeChild(this)   
        }
        
        private removeItem(evt: egret.TouchEvent): void{
            var target = evt.target
            var index = target.value
            if(gplayer.fuse.items[index]){
                var item = gplayer.fuse.items[index]
                this.selectPackage.addItem(item)
                gplayer.fuse.items[index] = null
                this.fuseInfo()
                this.showItems(this.selectPackage)
            }
        }
        
        private packageTouch(evt: egret.TouchEvent):void{
            var target = evt.target
            var packagetype = target.value
            if(packagetype === gplayer.bag.type){
                this.showItems(gplayer.bag)
            }else{
                this.showItems(gplayer.store)
            }
        }
        
        private showItems(pack:game.Package):void{
            this.itemsGroup.removeChildren()               
            for(var i in pack.items) {
                var item = pack.items[i]
                if(!item) {
                    continue
                }
                var btn: eui.Button = new eui.Button()
                btn.skinName = "resource/control/Button3Skin.exml"
                btn.width = 160
                this.itemsGroup.addChild(btn)
                var itemId = item.id
                var stack = item.stack
                btn.label = templateName[itemId] + 'X' + stack
                btn['value'] = itemId
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.setFuse,this)
                this.selectPackage = pack
            }
        }
        
        private setFuse(evt: egret.TouchEvent){
            var target = evt.target
            var itemId = target.value
            var items:any = null 
            if(!gplayer.fuse.items[0]) { 
                gplayer.fuse.items[0] = new game.ItemComponent({
                    id: itemId,
                    stack: 1
                })   
            } else if(!gplayer.fuse.items[1]){
                gplayer.fuse.items[1] = new game.ItemComponent({
                    id: itemId,
                    stack: 1
                })   
            } else if(!gplayer.fuse.items[2]) {
                gplayer.fuse.items[2] = new game.ItemComponent({
                    id: itemId,
                    stack: 1
                })              
            }else{
                return
            }            

            this.selectPackage.removeNumByItemId(itemId,1)
            this.showItems(this.selectPackage)
            this.fuseInfo()
        }
        
        private fuseInfo():void{
            var items = gplayer.fuse.items
            if(items[0]){
                this.item1Label.text = templateName[items[0].id]               
            } else {
                this.item1Label.text = ''
            }
            
            if(items[1]) {
                this.item2Label.text = templateName[items[1].id]
            } else {
                this.item2Label.text = ''
            }
            
            if(items[2]) {
                this.item3Label.text = templateName[items[2].id]
            } else {
                this.item3Label.text = ''
            }
            if(items[0] && items[1] && items[2]){
                this.fuseBtn.visible = true
            }else {
                this.fuseBtn.visible = false
            }
        }
        
        private startFuse(evt: egret.TouchEvent):void{
            var items = gplayer.fuse.items
            if(items[0] && items[1] && items[2]) {
                gplayer.fuse.startTime = new Date().getTime()
                gplayer.fuse.getItemId = 200005
                gplayer.fuse.items = []
                var child = new view.SmithyView()
                this.stage.addChild(child)
                this.parent.removeChild(this)                                  
            }
        }     
	} 
}
